package com.zbkj.crmeb.chant.controller;

import java.util.List;
import java.util.Set;

import javax.validation.constraints.NotEmpty;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.crmeb.core.common.CommonResult;
import com.utils.CrmebUtil;
import com.zbkj.crmeb.chant.dto.CategoryMatchDTO;
import com.zbkj.crmeb.chant.model.CategoryMatch;
import com.zbkj.crmeb.chant.service.ICategoryMatchService;
import com.zbkj.crmeb.chant.vo.CategoryMatchRequest;
import com.zbkj.crmeb.enums.BrandTypeEnum;
import com.zbkj.crmeb.enums.CategoryTypeEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 下游商品类目管理
 * @Description: 
 * @author daigb
 * @date 2021-04-13
 */
@RestController
@RequestMapping("api/admin/store/category/match")
@Api(tags = "下游商品类目管理")
public class CategoryMatchController {
    @Autowired
    ICategoryMatchService categoryMatchService;

    /**
     * 列表
     * @Description:  
     * @author daigb 
     * @param @param
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "列表")
    @GetMapping("/list")
    public CommonResult<Object> listEntity(@RequestParam(required = true) Integer type) {
        return CommonResult.success(categoryMatchService.list(Wrappers.<CategoryMatch>lambdaQuery()
                .eq(CategoryMatch :: getType, BrandTypeEnum.valueOf(type))
                .orderByAsc(CategoryMatch :: getCreateTime))
                );
    }
    
    /**
     * 分页查询
     * @Description:  
     * @author daigb 
     * @param @param categoryMatchRequest
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "分页")
    @PostMapping("/page")
    public CommonResult<Object> pageEntity(@RequestBody CategoryMatchRequest categoryMatchRequest) {
        return CommonResult.success(categoryMatchService.pageEntity(categoryMatchRequest));
    }
    
    /**
     * 通过id查询
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "通过id获取")
    @GetMapping("/{id}")
    public CommonResult<Object> getEntity(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id) {
        return CommonResult.success(categoryMatchService.getEntity(id));
    }
    
    /**
     * 新增
     * @Description:  
     * @author daigb 
     * @param @param categoryMatchDTO
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "新增")
    @PostMapping("/save")
    public CommonResult<Object> save(@RequestBody @Validated CategoryMatchDTO categoryMatchDTO) {
        if(null == categoryMatchDTO.getType()) {
            return CommonResult.validateFailed("分类类型不能为空");
        }
        // 1 标准分类
        if(CategoryTypeEnum.XUNWU != CategoryTypeEnum.valueOf(categoryMatchDTO.getType()) && null == categoryMatchDTO.getCategoryId()) {
            return CommonResult.validateFailed("关联寻物id不能为空");
        }
        
        return CommonResult.success(categoryMatchService.saveEntity(categoryMatchDTO));
    }
    
    @ApiOperation(value = "修改")
    @PutMapping("/update")
    public CommonResult<Object> update(@RequestBody CategoryMatchDTO categoryMatchDTO) {
        if(null == categoryMatchDTO.getType()) {
            return CommonResult.validateFailed("分类类型不能为空");
        }
        if(CategoryTypeEnum.XUNWU != CategoryTypeEnum.valueOf(categoryMatchDTO.getType()) && null == categoryMatchDTO.getCategoryId()) {
            return CommonResult.validateFailed("关联寻物id不能为空");
        }
        
        return CommonResult.success(categoryMatchService.updateEntity(categoryMatchDTO));
    }
    
    /**
     * 根据id删除
     * @Description:  
     * @author daigb 
     * @param @param id
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("/{id}")
    public CommonResult<Object> del(@ApiParam(value = "主键id", required = true) @PathVariable("id") Integer id) {
        return CommonResult.success(categoryMatchService.del(id));
    }

    /**
     * 批量删除
     * @Description:  
     * @author daigb 
     * @param @param ids
     * @param @return
     * @return CommonResult<Object>
     * @throws
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/remove")
    public CommonResult<Object> delBatch(@NotEmpty(message = "id不能为空") @RequestBody Set<Integer> ids) {
        return CommonResult.success(categoryMatchService.removeByIds(ids));
    }

    /**
     * 根据分类id集合获取分类数据
     * @param ids String id集合字符串
     * @since 2020-04-16
     */
    @ApiOperation(value = "根据id集合获取分类列表")
    @RequestMapping(value = "/list/ids", method = RequestMethod.GET)
    @ApiImplicitParam(name = "ids", value = "分类id集合")
    public CommonResult<List<CategoryMatch>> getByIds(@Validated @RequestParam(name = "ids") String ids) {
        return CommonResult.success(categoryMatchService.getByIds(CrmebUtil.stringToArray(ids)));
    }



}
